<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>父子组件通信案例（watch实现）</title>
</head>
<body>
  <div id="app">
    <cpn :number1="num1" 
         :number2="num2"
         @num1change="num1change"
         @num2change="num2change" />
  </div>

  <template id="cpn">
    <div>
      <h2> props:{{number1}} </h2>
      <h2>data:{{dnumber1}} </h2>
      <input type="text" v-model="dnumber1">
      <h2> props:{{number2}} </h2>
      <h2>data:{{dnumber2}} </h2>
      <input type="text" v-model="dnumber2">
    </div>
  </template>

  <script src="../js/vue.js"></script>
  <script>

    const app = new Vue({
      el: '#app',
      data: {
        num1: 0,
        num2: 1
      },
      methods: {
        num1change(value) {
          this.num1 = parseInt(value);
        },
        num2change(value) {
          this.num2 = parseInt(value);
        }
      },
      components: {
        cpn: {
          template: '#cpn',
          props: {
            number1: Number,
            number2: Number
          },
          data() {
            return {
              dnumber1: this.number1,
              dnumber2: this.number2
            }
          },
          methods: {
           
          },
          watch: {
            dnumber1(newValue) {
              this.dnumber2 = this.dnumber1 * 100;
              this.$emit('num1change', this.dnumber1)
              this.$emit('num2change', this.dnumber2)
            },
            dnumber2(newValue) {
              this.dnumber1 = this.dnumber2 / 100;
              this.$emit('num2change', this.dnumber2)
              this.$emit('num1change', this.dnumber1)
            }
          }
        }
      }
    })
    

  </script>
</body>
</html>